<?php
$type = '등록';
if($_GET['id']) {
    $type = "수정";
}
$title = "강의 평가".$type; 

$g4_path = "../"; 
require_once ($g4_path.'common.php');
require_once ('./teacher.head.php');
require_once ('./teacher.lib.php');

$tpl = new Template_;
$tpl->template_dir = './tpl';
$tpl->compile_dir = './tpl/compile';
$tpl->assign('type', $type);

$_datano = 0;
$_temp = $sqlWhere = NULL;
$_regcode = 'register';
$data = NULL;

if($_POST) { //등록,수정,삭제 시 처리
    $course = (int)$_POST['cate1'];
    $student = (int)$_POST['cate2'];
    $year = date('Y');
    $month = (int)$_POST['month'];
    $date = (int)$_POST['date'];
    $memo = addslashes($_POST['memo']); 
    $isabsent = $_POST['isabsent'] === "true" ? '1' : '0'; 
    $pron = (int)$_POST['pron'];
    $voca = (int)$_POST['voca'];
    $conv = (int)$_POST['conv'];
    $gram = (int)$_POST['gram'];

    if($isabsent === '0') {
        $pron = $voca = $conv = $gram = $memo = NULL;
    }

    $sql = "SET `course`='$course', `student`='$student', `year`='$year', `month`='$month', `date`='$date', `pron`='$pron', `voca`='$voca', `gram`='$gram', `conv`='$conv', `memo`='$memo', `isabsent`='$isabsent'"; 
   
    switch($_POST['regcode']) {
        case 'modify': // 강의 수정
            $sql = "UPDATE coursevaluation ".$sql;
            $sqlWhere = " WHERE no = '".$_POST['datano']."' ";
            break;
        case 'register': // 강의 추가
            $sql = "INSERT INTO coursevaluation ".$sql;
            break;
        case 'delete': // 강의 삭제
            $sql = "DELETE FROM coursevaluation WHERE no = '".$_POST['datano']."' LIMIT 1"; 
            break;
        default:
            alert('오류 : 잘못된 접근입니다.', './courseManage.php'); 
    }
    $ret = sql_query($sql.$sqlWhere) or alert('오류 : SQL오류입니다.:'.mysql_error(), './courseValuation.php'); 
    
    switch($_POST['config']) {
        case 'add':
            goto_url('./courseValuationDetail.php?add'); 
        break;
        case 'delete':
            alert('성공적으로 삭제되었습니다.', './courseValuation.php'); 
        break;
        default:
            $page = 1; 
            if($_POST['datano']) {
                $res = sql_fetch("select count(*) as cnt from coursevaluation where no > ".$_POST['datano']);
                $pageRet = $res[cnt]/$perPage+1;
                $page = (int)$pageRet;
            }
            goto_url('./courseValuation.php?page='.$page); 
    }
}

if(isset($_GET['id'])) { //수정시 (id값이 넘어왔을때)
    $_regcode = 'modify';
    $_datano = $_GET['id']; 
    $data = getDb('coursevaluation', $_datano, 'no');  
    $data['memo'] = htmlspecialchars(stripslashes($data['memo'])); 
    $cate1 = $data['course'];
    $cate2 = $data['student'];

    if(!$data['no']) {
        alert('자료가 존재하지 않습니다.');
    }
}

    $scoreArray = array('10'=>'상', '5'=>'중', '1'=>'하');
    $data['pronOptions'] = @_makeOptionList($scoreArray, $data['pron']);
    $data['vocaOptions'] = @_makeOptionList($scoreArray, $data['voca']);
    $data['convOptions'] = @_makeOptionList($scoreArray, $data['conv']);
    $data['gramOptions'] = @_makeOptionList($scoreArray, $data['gram']);
    $tpl->assign('data', $data);
$tpl->assign('regcode', $_regcode); 
require_once ('./teacher.head.php');

$tpl->define('basic',"courseValuationDetail.html");
$tpl->print_('basic');

require_once('inc/teacher_category.php');
require_once('teacher.foot.php');
